/**************************************************************************************
 *
 * @(#) CreateImageAnchor.java
 * @Package com.jgdt.jgdtkasannong_university_gene_backend.utils
 **************************************************************************************/
package com.jgdt.jgdtkasannong_university_gene_backend.utils.excel;
/**
 * @description: 表格图片定位工具类
 * @author: huangAn
 * @createDate: 2025/04/29
 */

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jgdt.jgdtkasannong_university_gene_backend.config.enums.SysValueSetEnums;
import com.jgdt.jgdtkasannong_university_gene_backend.service.sys.SysValueSetService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.FileSystemResource;

import java.io.InputStream;

/**
 * @description:
 * @author: huangAn
 * @createDate: 2025/04/29
 */
@Slf4j
public class ExcelImageUtils {


    public static ClientAnchor createImageAnchor(CreationHelper helper,
                                                 int col, int row, int widthInCells, int heightInCells) {
        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setCol1(col);
        anchor.setRow1(row);
        anchor.setCol2(col + widthInCells);
        anchor.setRow2(row + heightInCells);
        return anchor;
    }

    public static ClientAnchor createImageAnchor(CreationHelper helper,
                                                 int col, int row) {
        return createImageAnchor(helper, col, row, 1, 1);
    }

    public static String getCellValueAsString(Row row, int column) {
        if (row == null) {
            return "";
        }

        Cell cell = row.getCell(column, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
        if (cell == null) {
            return "";
        }

        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                return String.valueOf(cell.getNumericCellValue());
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            default:
                return "";
        }
    }

    public static String getCellValueAsString(Cell cell) {
        if (cell == null) {return "";}
        DataFormatter formatter = new DataFormatter();
        return formatter.formatCellValue(cell);
    }

//    /**
//     * Excel 表格浮动位移
//     * @param workbook 位移表格对象
//     * @param sheet 位移表格对象
//     * @param imgUrl 位移图片url
//     * @param col 位移列数
//     * @param row 位移行数
//     * @param widthlnCells 图片跨列数
//     * @param heightlnCells 图片跨行数
//     */
//    public static void imgDisplacement(XSSFWorkbook workbook, XSSFSheet sheet,String imgUrl,int col,int row,int widthlnCells,int heightlnCells){
//        try{
//            if(StringUtils.isEmpty(imgUrl)){
//                log.warn("imgDisplacement,warn:获取不到图片,图片地址url:"+imgUrl);
//                return;
//            }
//            String tempImgCataloguePrefixHead=sysValueSetService.queryPriorityValueByCode(SysValueSetEnums.TEMP_IMG_CATALOGUE_PREFIX_HEAD.getCode());
//            String chageImgUrl= tempImgCataloguePrefixHead+imgUrl;
//            log.debug("chageImgUrl"+chageImgUrl);
//            // 3. 加载图片文件
//            // 对于实际应用，你可能需要从文件系统绝对路径加载
//            FileSystemResource imgFile = new FileSystemResource(chageImgUrl);
//            InputStream inputStream = imgFile.getInputStream();
//            byte[] bytes = IOUtils.toByteArray(inputStream);
//            int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
//            inputStream.close();
//
//
//            // 4. 创建绘图对象并添加图片
//            CreationHelper helper = workbook.getCreationHelper();
//            Drawing<?> drawing = sheet.createDrawingPatriarch();
//            // 创建锚点 (指定图片位置)
//            //定位 C4 单元格 2 3 跨2列1行
//            workbook.setActiveSheet(0); // 激活第一个工作表
//            ClientAnchor anchor = ExcelImageUtils.createImageAnchor(helper,col,row,widthlnCells,heightlnCells);
//            // 添加图片
//            drawing.createPicture(anchor, pictureIdx);
//        }catch (Exception e){
//            e.printStackTrace();
//            log.error("imgDisplacement,err:"+e.getMessage());
//        }
//    }
}




